﻿function AzureStorageQueuesApi(credentials, channel) {
    var self = this;
    
    function getBaseUrl() {
        return credentials.getQueuesUrl();
    }

    function onError(request) {
        var responeText = request.responseText;
    }

    function execute(request, options) {
        request.sign(credentials);

        return channel.send(request, function (data, req) {
            if (options && options.success) options.success(data, req);
        }, function (data, req) {
            onError(req);
            var action = 'notify';
            if (options && options.error) action = options.error(data, req);
            if (action == 'notify') {
                notificationArea.showError(req.statusText);
            }
        }, function () {
        });
    }
    
    function getUri(relPath) {
        relPath = relPath || '';
        return new Uri().parse(getBaseUrl() + relPath);
    }
    
    var requests = {
        createQueue: function (options) {
            var queue = options.queue;
            var request = new AzureRequest();
            request.verb = 'PUT';
            request.uri = getUri(queue);
            request.headers['Content-Length'] = '0';
            request.signatureType = 'SharedKey';
            return request;
        },
        list: function () {
            var request = new AzureRequest();
            request.verb = 'GET';
            request.uri = getUri();
            request.signatureType = 'SharedKey';
            request.addQuery('comp', 'list');
            request.addQuery('timeout', 90);
            request.addQuery('include', 'metadata');
            return request;
        },
        deleteQueue: function (options) {
            var queue = options.queue;
            var request = new AzureRequest();
            request.verb = 'DELETE';
            request.uri = getUri(queue);
            request.signatureType = 'SharedKey';
            return request;
        },
        getMessages: function (options) {
            var request = new AzureRequest();
            request.verb = 'GET';
            request.uri = getUri(options.queue + '/messages');
            request.signatureType = 'SharedKey';
            request.addQuery('visibilitytimeout', options.visibilityTimeout);
            request.addQuery('numofmessages', '32');
            return request;
        },
        peekMessages: function (options) {
            var request = new AzureRequest();
            request.verb = 'GET';
            request.uri = getUri(options.queue + '/messages');
            request.signatureType = 'SharedKey';
            request.addQuery('peekonly', 'true');
            request.addQuery('numofmessages', '32');
            return request;
        },
        deleteMessage: function (options) {
            var request = new AzureRequest();
            request.verb = 'DELETE';
            request.uri = getUri(options.queue + '/messages/' + options.messageId);
            request.signatureType = 'SharedKey';
            request.addQuery('popreceipt', options.popreceipt);
            return request;
        },
        putMessage: function (options) {
            var content = options.content;
            var contentBytes = new UTF8().getBytes(content);
            
            var request = new AzureRequest();
            request.verb = 'POST';
            request.uri = getUri(options.queue + '/messages');
            request.headers['Content-Length'] = contentBytes.length;
            request.signatureType = 'SharedKey';
            request.data = options.content;
            return request;
        },
        clearMessages: function (options) {
            var request = new AzureRequest();
            request.verb = 'DELETE';
            request.uri = getUri(options.queue + '/messages');
            request.signatureType = 'SharedKey';
            return request;
        },
        getServiceProperties: function () {
            var request = new AzureRequest();
            request.verb = 'GET';
            request.uri = getUri();
            request.signatureType = 'SharedKey';
            request.addQuery('restype', 'service');
            request.addQuery('comp', 'properties');
            return request;
        },
        setServiceProperties: function (options) {
            var content = options.content;
            var contentBytes = new UTF8().getBytes(content);
            
            var request = new AzureRequest();
            request.verb = 'PUT';
            request.uri = getUri();
            
            request.headers['Content-Type'] = 'application/xml';
            request.headers['Content-Length'] = contentBytes.length;
            request.data = options.content;
            
            request.signatureType = 'SharedKey';
            request.addQuery('restype', 'service');
            request.addQuery('comp', 'properties');
            return request;
        },
        setQueueMetadata: function (options) {
            var queue = options.queue;

            var request = new AzureRequest();
            request.verb = 'PUT';
            request.uri = getUri(queue);
            request.headers['Content-Length'] = '0';
            request.version = '2012-02-12';
            request.addQuery('comp', 'metadata');
            if (options.metadata) {
                for (var i = 0; i < options.metadata.length; i++) {
                    var row = options.metadata[i];
                    request.headers['x-ms-meta-' + row.name] = row.value;
                }
            }
            request.signatureType = 'SharedKey';
            return request;
        }
    };

    function listQueues(options) {
        var request = requests.list(options);
        execute(request, options);
    }

    function createQueue(options) {
        var request = requests.createQueue(options);
        execute(request, options);
    }

    function removeQueue(options) {
        var request = requests.deleteQueue(options);
        execute(request, options);
    }

    function getMessages(options) {
        var request = requests.getMessages(options);
        execute(request, options);
    }

    function peekMessages(options) {
        var request = requests.peekMessages(options);
        execute(request, options);
    }
    
    function deleteMessage(options) {
        var request = requests.deleteMessage(options);
        execute(request, options);
    }

    function putMessage(options) {
        var request = requests.putMessage(options);
        execute(request, options);
    }
    
    function clearMessages(options) {
        var request = requests.clearMessages(options);
        execute(request, options);
    }
    
    function getServiceProperties(options) {
        var request = requests.getServiceProperties(options);
        execute(request, options);
    }
    
    function setQueueMetadata(options) {
        var request = requests.setQueueMetadata(options);
        execute(request, options);
    }
    
    function setServiceProperties(options) {
        var request = requests.setServiceProperties(options);
        execute(request, options);
    }
    
    self.requests = requests;
    self.listQueues = listQueues;
    self.createQueue = createQueue;
    self.removeQueue = removeQueue;
    self.getMessages = getMessages;
    self.peekMessages = peekMessages;
    self.deleteMessage = deleteMessage;
    self.putMessage = putMessage;
    self.clearMessages = clearMessages;
    self.getServiceProperties = getServiceProperties;
    self.setServiceProperties = setServiceProperties;
    self.setQueueMetadata = setQueueMetadata;
}